package com.yicong.ants.manager;

import com.cchao.simplelib.Const;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseActivity;
import com.cchao.simplelib.util.StringHelper;
import com.yicong.ants.Constants;
import com.yicong.ants.bean.ad.AntsAd;
import com.yicong.ants.manager.ad.AdManager;
import com.yicong.ants.manager.ad.AntsAdHelper;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.ui.find.YcWebViewActivity;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cch
 * @version 10-21
 */
public class EnergyHelper {
    /*广告类型，广告视频30，短视频50，商城1036*/
    public static String mEnergyType;

    /**
     * 激励视频活跃度
     *
     * @param baseActivity
     */
    public static void onStartRewardVideo(BaseActivity baseActivity) {
        AdManager.preloadAndShowReward(baseActivity, Constants.Event.Ants_Ad_Complete);
    }

    /**
     * 活跃度任务完成
     */
    public static void doneEnergyTask(BaseActivity baseActivity) {
        if (StringHelper.isEmpty(mEnergyType)) {
            return;
        }
        baseActivity.showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("type", mEnergyType);
        baseActivity.addSubscribe(RetrofitHelper.getApis().energyTaskReward(map)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                baseActivity.hideProgress();
                if (respBean.isCodeFail()) {
                    UiHelper.showConfirmDialog(baseActivity, respBean.getMsg(), null);
                    return;
                }
                Dialogs.showEnergySuc(baseActivity, respBean.getMsg(), null);
                UserManager.refreshUserInfo();
            }, RxHelper.getHideProgressConsumer(baseActivity)));
        mEnergyType = "";//防止同个type多次请求
    }

    /**
     * 活跃度商品链接
     */
    public static void energyTaskMall(BaseActivity baseActivity) {
        baseActivity.addSubscribe(RetrofitHelper.getApis().energyTaskMall(Collections.emptyMap())
            .compose(RxHelper.toMain())
            .subscribe(resp -> {
                if (resp.isCodeFail()) {
                    UiHelper.showToast(resp.msg);
                    return;
                }
                AntsAd antsAd = resp.getData();
                AntsAdHelper.setAdInfo(antsAd, AntsAdHelper.Position.energy_task_web);
                Router.turnTo(baseActivity, YcWebViewActivity.class)
                    .putExtra(Const.Extra.Web_View_Url, antsAd.getUrl())
                    .putExtra(Const.Extra.Web_View_Title_Visible, false)
                    .start();
            }));
    }

}
